/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.Service
*        文件名:             FileService.svc.cs
*        创建系统时间:       2012/11/2 19:07:04
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.Service
{
    using System;
    using System.Data;
    using System.Reflection;
    using System.ServiceModel;
    using ECP.DataAccess;
    using ECP.DataAccess.Utilities;
    using ECP.Interface;
    using ECP.Model;
    using ECP.Utilities;

    [ServiceBehavior(Namespace = "http://jiangguang.net.cn/")]
    public partial class FileService : MarshalByRefObject, IFileService
    {
        private static FileService instance = null;
        private static object locker = new object();

        public string Add(BaseUserInfo userInfo, string folderID, string fileName, byte[] file, string description, string category, bool enabled, out string statusCode, out string statusMessage)
        {
            statusCode = string.Empty;
            statusMessage = string.Empty;
            string str = string.Empty;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseFileDao dao = new BaseFileDao(dbHelper, userInfo);
                str = dao.Add(folderID, fileName, file, description, category, enabled, out statusCode);
                statusMessage = dao.GetStateMessage(statusCode);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return str;
        }

        public int BatchDelete(BaseUserInfo userInfo, string[] ids)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseFileDao dao = new BaseFileDao(dbHelper, userInfo);
                for (int i = 0; i < ids.Length; i++)
                {
                    num += dao.Delete(ids[i]);
                }
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int BatchMoveTo(BaseUserInfo userInfo, string[] ids, string folderID)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseFileDao dao = new BaseFileDao(dbHelper, userInfo);
                for (int i = 0; i < ids.Length; i++)
                {
                    num += dao.MoveTo(ids[i], folderID);
                }
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int BatchSave(BaseUserInfo userInfo, DataTable dataTable)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                num = new BaseFileDao(dbHelper, userInfo).BatchSave(dataTable);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int Delete(BaseUserInfo userInfo, string id)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                num = new BaseFileDao(dbHelper, userInfo).Delete(id);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int DeleteByFolder(BaseUserInfo userInfo, string folderID)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                num = new BaseFileDao(dbHelper, userInfo).DeleteByFolder(folderID);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public byte[] Download(BaseUserInfo userInfo, string id)
        {
            byte[] buffer = null;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                buffer = new BaseFileDao(dbHelper, userInfo).Download(id);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return buffer;
        }

        public bool Exists(BaseUserInfo userInfo, string folderID, string fileName)
        {
            bool flag = false;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                flag = new BaseFileDao(dbHelper, userInfo).Exists(BaseFileTable.FieldFolderID, folderID, BaseFileTable.FieldFileName, fileName);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return flag;
        }

        public DataTable Get(BaseUserInfo userInfo, string id)
        {
            DataTable table = new DataTable(BaseFileTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                table = new BaseFileDao(dbHelper, userInfo).Get(id);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return table;
        }

        public DataTable GetListByFolder(BaseUserInfo userInfo, string folderID)
        {
            DataTable listByFolder = new DataTable(BaseFileTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                listByFolder = new BaseFileDao(dbHelper, userInfo).GetListByFolder(folderID);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return listByFolder;
        }

        public void Load()
        {
        }

        public int MoveTo(BaseUserInfo userInfo, string id, string folderID)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                num = new BaseFileDao(dbHelper, userInfo).MoveTo(id, folderID);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int Rename(BaseUserInfo userInfo, string id, string newName, bool enabled, out string statusCode, out string statusMessage)
        {
            statusCode = string.Empty;
            statusMessage = string.Empty;
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseFileEntity fileEntity = new BaseFileEntity();
                BaseFileDao dao = new BaseFileDao(dbHelper, userInfo);
                DataTable dataTable = dao.Get(id);
                fileEntity.GetFrom(dataTable);
                fileEntity.FileName = newName;
                fileEntity.Enabled = enabled;
                num = dao.Update(fileEntity, out statusCode);
                statusMessage = dao.GetStateMessage(statusCode);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public DataTable Search(BaseUserInfo userInfo, string search)
        {
            DataTable table = new DataTable(BaseFileTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                table = new BaseFileDao(dbHelper, userInfo).Search(search);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return table;
        }

        public int Update(BaseUserInfo userInfo, string id, string folderID, string fileName, string description, bool enabled, out string statusCode, out string statusMessage)
        {
            statusCode = string.Empty;
            statusMessage = string.Empty;
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseFileDao dao = new BaseFileDao(dbHelper, userInfo);
                num = dao.Update(id, folderID, fileName, description, enabled, out statusCode);
                statusMessage = dao.GetStateMessage(statusCode);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int UpdateFile(BaseUserInfo userInfo, string id, string fileName, byte[] file, out string statusCode, out string statusMessage)
        {
            statusCode = string.Empty;
            statusMessage = string.Empty;
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseFileDao dao = new BaseFileDao(dbHelper, userInfo);
                num = dao.UpdateFile(id, fileName, file, out statusCode);
                statusMessage = dao.GetStateMessage(statusCode);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public string Upload(BaseUserInfo userInfo, string folderID, string fileName, byte[] file, bool enabled)
        {
            string str = string.Empty;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                str = new BaseFileDao(dbHelper, userInfo).Upload(folderID, fileName, file, enabled);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return str;
        }

        public static FileService Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = new FileService();
                        }
                    }
                }
                return instance;
            }
        }
    }
}